package com.hcss.core.common.logsmgt.dao;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.hcss.core.common.logsmgt.model.LoginLog;
import com.hcss.core.mvc.BaseDao;
import com.hcss.core.mvc.page.Pager;


/** 
 * @ClassName: LoginLogDao 
 * @Description: TODO 登陆日志DAO
 * @author lvfang
 * @date 2013-12-9 下午4:40:56 
 *  
 */
@Repository("loginDao")
public class LoginLogDao  extends BaseDao{
	
	private static final String TBLPREFIX = "t_loginLog";		
	public static String table() {
	       return TBLPREFIX;
	}
	
	/** 
	* <p> addLoginLog TODO增加登陆日志</p>
	* @param log
	* @return  int 设定文件 
	*/
	public int addLoginLog(final LoginLog log){
		if (log == null) {
			return 0;
		}
		StringBuffer sql = new StringBuffer();
		sql.append("INSERT INTO t_loginLog");
		sql.append(" (loginTime, loginType, userId, loginIp) VALUES (?, ?, ?, ?)");
		Object[] params = new Object[]{
			log.getLoginTime(),
			log.getLoginType(),
			log.getUserId(),
			log.getLoginIp()
		};
		try {
			this.hibernateTemplate.saveOrUpdate("t_loginLog", log);
			return 1;
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//return this.insertSelective(sql.toString(), params);
		return 0;
	}
	
	/**
	 * 返回所有登陆信息
	 * @return
	 * @throws Exception
	 */
	public Pager getAllLoginLog(Pager page,String userName)throws Exception{
		StringBuffer sql = new StringBuffer();
        sql.append("select a.* , u.userName from t_loginLog a ");
        sql.append("left join t_userinfo u on a.userId = u.id");
        sql.append(" where 1=1 and u.isDeleted = 'n' ");
        if(userName != null && !"".equals(userName)){
        	sql.append(" and u.userName = '"+userName +"' ");
        }
        RowMapper<?> rowMapper = new BeanPropertyRowMapper<LoginLog>(LoginLog.class);
        return this.getPageList(sql.toString(), page, rowMapper);
	}

}
